<?php
namespace Home\Controller;
use Think\Controller;
class EditpersonController extends Controller {
    // 后台登录页
    public function index($id){
        if(isset($_SESSION['admin'])){ 
            $position = D('position');
            $positionList = $position->select();            
            $this->assign('arrP',$positionList);
            $sector = D('sector');
            $sectorList = $sector->select();            
            $this->assign('arrS',$sectorList);
            $person = D("person");            
            $personList = $person->where("id='$id'")->select();
            $this->assign('personId',$id);
            $personnumber = $personList[0]['number'];
            $this->assign('personNumber',$personnumber);
            $personname = $personList[0]['name'];
            $this->assign('personName',$personname);
            $personage = $personList[0]['age'];
            $this->assign('personAge',$personage);
            $personsex = $personList[0]['sex'];
            $this->assign('personSex',$personsex);
            $personphone = $personList[0]['phone'];
            $this->assign('personPhone',$personphone);
            $personaddress = $personList[0]['address'];
            $this->assign('personAddress',$personaddress);
            $personsalary = $personList[0]['salary'];
            $this->assign('personSalary',$personsalary);
            $sector_id = $personList[0]['sector_id'];
            $this->assign('sector_id',$sector_id);
            $position_id = $personList[0]['position_id'];
            $this->assign('position_id',$position_id);               

            $this->display();
        }else{
            $url = 'http://localhost/person/index.php/Home/Index/manage';
            header("location: $url");
        }
    }

     //员工信息编辑
    public function personEdit($id,$number,$name,$age,$sex,$phone,$address,$salary,$positionId,$sectorId){
        if($id == null||$number == null || $name  == null || $age  == null|| $sex  == null|| $phone == null|| $address == null|| $positionId == null){
            $result = array(
                'code'=> '1',
                'ext'=> 'error'
            );
            echo json_encode($result);
            return;
        }
        if($salary==null){
             $position = D("position");
             $salaryP = $position->where("id='$positionId'")->field('salary')->select();
             $salary = $salaryP[0]['salary']; 
        }     
        
        $person = M("person");
        $data['id'] = $id;
        $data['number'] = $number;
        $data['name'] = $name;
        $data['age'] = $age;
        $data['sex'] = $sex;
        $data['phone'] = $phone;
        $data['address'] = $address;
        $data['salary'] = $salary;
        $data['sector_id'] = $sectorId;
        $data['position_id'] = $positionId;
        $success = $person->save($data);
        if($success){
            $result = array(
                'code'=> '0',
                'ext'=> 'success',
            );
            echo json_encode($result);
        }else {
            $result = array(
                'code'=> '1',
                'ext'=> 'fail'
            );
            echo json_encode($result);
        } 
    }
      //退出
    public function doLogout(){
        $_SESSION = array();
        if(isset($_COOKIE[session_name()])){
            setcookie(session_name(),'',time()-1,'/');
        }
        session_destroy();
        $this->redirect('Index/index');
    }
}
